package com.qianyu.core.task;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qianyu.core.api.ApiCommon;
import com.qianyu.core.entity.bean.CronTask;
import com.qianyu.core.entity.bean.CronTaskItem;
import com.qianyu.core.entity.bean.QqAccount;
import com.qianyu.core.service.CornTaskItemService;
import com.qianyu.core.service.CornTaskService;
import com.qianyu.core.service.QqAccountService;
import com.qianyu.core.util.CacheKeyUtil;
import com.qianyu.core.util.RedisUtil;
import com.qianyu.core.util.TimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class ApplyQqAccountTask {

    private final RedisUtil redisUtil;

    private final QqAccountService qqAccountService;

    private final ApiCommon apiCommon;

    private final CornTaskItemService cornTaskItemService;

    private final CornTaskService cornTaskService;

    /**
     * 每秒处理队列里qq账号并保存到数据库
     */
    @Async
    @Scheduled(cron = "0/1 * * * * ?")
    public void applyQqAccount() {
        if (redisUtil.hasKey(CacheKeyUtil.applyQqAccountSet())) {
            QqAccount account = new QqAccount();
            Object o = redisUtil.rGet(CacheKeyUtil.applyQqAccountSet());
            JSONObject object = (JSONObject) o;
            account.setQq(object.getString("qq"));
            account.setLoginSecret(object.getString("password"));
            account.setCity(object.getString("city"));
            account.setProvince(object.getString("province"));
            account.setPhone(object.getString("phone"));
            account.setCache(object.getString("cache"));
            //获取长效代理
            QqAccount qqAccount = apiCommon.applyProxy(account, 0);
            //获取信用分
            apiCommon.queryCommonCredit(qqAccount, 0);
            //获取Q币
            apiCommon.fetchCachedQbQuantity(qqAccount, 0);
            qqAccountService.save(qqAccount);
        }

    }


    /**
     * 每十分钟刷新队列里qq账号的qb并保存到数据库
     */
    @Async
//    @Scheduled(cron = "0/1 * * * * ?")
    @Scheduled(cron = "0 */10 * * * ?")
    public void flushQb() {
        List<CronTask> list = cornTaskService.list();
        for (CronTask task : list) {
            if (redisUtil.hasKey(CacheKeyUtil.flushQbSet(task.getId()))) {

                QqAccount qqAccount = (QqAccount) redisUtil.rGet(CacheKeyUtil.flushQbSet(task.getId()));
                //获取Q币
                apiCommon.fetchCachedQbQuantity(qqAccount, 0);
                qqAccountService.updateById(qqAccount);
                cornTaskItemService.update(Wrappers.<CronTaskItem>lambdaUpdate().set(CronTaskItem::getStatus, 1)
                        .set(CronTaskItem::getIsEnd,1).eq(CronTaskItem::getTaskId, task.getId()));
            }
        }
    }

    /**
     * 每分钟，修改过期的账号
     */
    @Async
//    @Scheduled(cron = "0 */9 * * * ?")
    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨0点执行
    public void timeoutAccount() {
        //所有超时账号集合
        List<QqAccount> accounts = new ArrayList<>();
        //获取正在租用的账号
        List<QqAccount> list = qqAccountService.list(Wrappers.<QqAccount>lambdaQuery().eq(QqAccount::getRentStatus, 1));
        for (QqAccount account : list) {
            if (TimeUtil.unixTime() > account.getRentExpire()) {
                account.setRentStatus(2);
                accounts.add(account);
            }
        }
        if (ObjectUtil.isNotEmpty(accounts)) {
            qqAccountService.updateBatchById(accounts);
        }
    }
}
